package us.icitap.timsportal.management;

import java.util.List;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

import us.icitap.timsportal.ejb.PortalBeanLocal;
import us.icitap.timsportal.entity.Officer;
import us.icitap.timsportal.resources.TimsPortalLabels;
import us.icitap.timsportal.web.custom.PNativeButton;
import us.icitap.timsportal.web.custom.PTextField;
import us.icitap.timsportal.web.custom.PWindow;

public class OfficerWindow extends PWindow implements ClickListener, ValueChangeListener{
	
	private PTextField searchField;
	private PNativeButton searchButton;
	private Table table;
	private OfficerForm officerForm;
	private BeanItemContainer<Officer> dataSource;
	private PortalBeanLocal portalBean;
	private UserForm userForm;
	
	public static final Object[] NATURAL_COL_ORDER = new Object[] {
		"officerId", "forename", "fatherName", "surname", "function" };
	
	public static final String[] COL_HEADERS_ENGLISH = new String[] {
		TimsPortalLabels.getLabel("label.officerId"),
		TimsPortalLabels.getLabel("label.name"),
		TimsPortalLabels.getLabel("label.fatherName"),
		TimsPortalLabels.getLabel("label.surname"),
		TimsPortalLabels.getLabel("label.function")};
	
	
	public OfficerWindow(OfficerForm officerForm, UserForm userForm){
		setWidth("60%");
		setModal(true);
		
		this.officerForm = officerForm;
		this.userForm = userForm;
		
		searchField = new PTextField(); 
		searchField.setWidth("200px");
		searchField.setRequired(true);
		searchField.setInputPrompt(TimsPortalLabels.getLabel("label.fullName"));
		
		searchButton = new PNativeButton(TimsPortalLabels.getLabel("btn.search"), this);
		searchButton.setWidth("100px");
		dataSource = new BeanItemContainer<Officer>(Officer.class);
		table = new Table("", dataSource);
		table.addListener(this);
		table.setSizeFull();
		table.setSelectable(true);
		table.setImmediate(true);
		
		table.setVisibleColumns(NATURAL_COL_ORDER);
		table.setColumnHeaders(COL_HEADERS_ENGLISH);

		
		HorizontalLayout hl = new HorizontalLayout();
//		hl.setSizeFull();
		hl.setSpacing(true);
		hl.addComponentAsFirst(searchField);
		hl.addComponent(searchButton);
		
		VerticalLayout layout = (VerticalLayout) this.getContent();
		layout.setSpacing(true);
		layout.addComponentAsFirst(hl);
		layout.addComponent(table);
	}	
	
	@Override
	public void buttonClick(ClickEvent event) {
		// TODO Auto-generated method stub
		if(event.getButton()==searchButton){
			if(!searchField.isValid())
				return;
			String[] vals = ((String) searchField.getValue()).split(" ");
			if(vals.length!=2)
				return;
			List<Officer> list = portalBean.getRmisOfficerByName(vals[0], vals[1]);
			this.dataSource.removeAllItems();
			this.dataSource.addAll(list);

		}
		
	}
	
	@Override
	public void valueChange(ValueChangeEvent event) {
		// TODO Auto-generated method stub
		Officer officer = (Officer) event.getProperty().getValue();
		officerForm.setOfficer(officer);
		userForm.setNoOfficer(officer==null);
		this.getParent().removeWindow(this);
	}
	
	@Override
	public void attach(){
		super.attach();
		this.portalBean = getApplication().getPortalBean();
	}

	public void clear() {
		// TODO Auto-generated method stub
		searchField.setValue("");
		dataSource.removeAllItems();
	}

}
